package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.SettingService;
import util.ServletHelp;
import util.StringUtils;

/**
 * 系统设置Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class SettingAction extends BaseAction {

	/**
	 * 系统设置Service
	 * */
	private SettingService settingService = null;

	/**
	 * @param settingService 系统设置Service
	 */
	public void setSettingService(SettingService settingService) {
		this.settingService = settingService;
	}
	
	/**
	 * 获得系统设置信息
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward getSetting(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		// 返回结果
		JSONObject res = new JSONObject();
		// 取得系统设置信息
		JSONObject setting = settingService.getSetting();
		if(null != setting){
			res = setting;
		}
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	
	/**
	 * 获得系统设置信息
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward saveSetting(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获得参数
		String settingStr = request.getParameter("setting");
		if(StringUtils.isNotEmpty(settingStr)){
			JSONObject setting = JSONObject.fromObject(settingStr);
			// 保存
			settingService.saveSetting(setting);
		}
		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
}
